﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MySqlSugar;

namespace ConsoleApp1.DB
{

        public class BaseRepository<T> where T : class, new()


        {
            public void Add(T entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.Insert(entity);
                }
            }

            public void BatchAdd(List<T> entities)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.SqlBulkCopy<T>(entities);
                }
            }
            public void Add(List<T> entities)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.InsertRange(entities);
                }
            }

            public void Delete(Expression<Func<T, bool>> exp)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.Delete<T>(exp);
                }
            }

            public void Delete(T entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.Delete<T>(entity);
                }

            }

            public void Update(Expression<Func<T, bool>> identityExp, T entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.Update(entity, identityExp);
                }
            }

            public IQueryable<T> Find(Expression<Func<T, bool>> exp = null)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    if (exp == null)
                    {
                        return db.Queryable<T>().ToList().AsQueryable();
                    }
                    else
                    {
                        return db.Queryable<T>().Where(exp).ToList().AsQueryable();
                    }


                }
            }



            public IQueryable<T> PageFind(int pageindex = 1, int pagesize = 10, Func<T, T> orderby = null,
                Expression<Func<T, bool>> exp = null)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    return db.Queryable<T>().Where(exp).ToPageList(pageindex, pagesize).OrderBy(orderby).AsQueryable();
                }
            }

            public T FindSingle(Expression<Func<T, bool>> exp = null)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    return db.Queryable<T>().Where(exp).First();
                }
            }

            public int GetCount(Expression<Func<T, bool>> exp = null)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    if (exp == null)
                    {
                        return db.Queryable<T>().Count();
                    }
                    else
                    {
                        return db.Queryable<T>().Where(exp).Count();
                    }


                }
            }

            public bool IsExist(Expression<Func<T, bool>> exp)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    return db.Queryable<T>().Any(exp);
                }
            }


            public void Update(T entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.Update(entity);
                }
            }

            public void Update(Expression<Func<T, bool>> where, Expression<Func<T, T>> entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {

                    db.Update(entity, where);
                }
            }
            public void Update(object obj, Expression<Func<T, bool>> entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {

                    db.Update(obj, entity);
                }
            }
            public void Update(string setvalue, Expression<Func<T, bool>> where, T entity)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.Update(setvalue, where, entity);
                }
            }

            public void BulkUpdate(List<T> ts)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.SqlBulkReplace(ts);
                }
            }
            public void BulkUpdateLittleDatas(List<T> ts)
            {
                using (var db = DB.DBContext.GetInstance())
                {
                    db.UpdateRange(ts);
                }
            }

            
        }

}
